/*
 * @Author: juneChen && juneswoole@163.com
 * @Date: 2023-06-02 15:45:53
 * @LastEditors: juneChen && juneswoole@163.com
 * @LastEditTime: 2023-08-03 17:03:39
 * @Description: 图片验证码
 * 
 */

import { getCaptchaPicture } from '@api/public';

export default {
    namespaced: true,
    state: () => ({
        picture: "",
        key: "",
        loading: false,
    }),
    mutations:{
        /**
         * 设置验证码图片
         * @param {Object} state vuex state
         * @param {String} base64  图片流
         */
        setPicture(state, base64){
            state.picture = base64;
        },
        /**
         * 设置验证码KEY
         * @param {Object} state vuex state
         * @param {String} key   验证码KEY
         */
        setKey(state, key){
            state.key = key;
        },
        /**
         * 设置是否在加载
         * @param {Object} state vuex state
         * @param {boolean} bool   
         */
        setLoading(state, bool){
            state.loading = bool;
        }
    },
    actions: {
        /**
         * @description 刷新图片验证码
         * @param {Object} dispatch vuex dispatch
         */
        refreshPictureCaptcha({ state, commit }){
            if(state.loading){
                return false;
            }
            commit('setLoading',true);
            getCaptchaPicture().then(res => {
                commit('setLoading',false);
                commit('setPicture',res.base64);
                commit('setKey',res.key);
            })
        }
    }
};
